<template>
  <div class="main-app">
    <el-card class="box-card">
      <el-form :inline="true" :model="formInline" class="form-inline">
        <el-form-item label="名称:">
          <el-input v-model="formInline.content" clearable />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="search">
            查询
          </el-button>
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-refresh-right" @click="reset">
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <div class="show-result">

      <!-- 加载动态菜单 -->
      <el-form :inline="true" class="form-inline">
        <el-form-item v-for="item in toolbtn" :key="item.id" >
          <el-button type="primary" :icon='item.meta.icon' @click="showViews(item.name)">
            {{ item.meta.title }}
          </el-button>
        </el-form-item>
      </el-form>

      <!-- <el-form :inline="true" class="form-inline">
        <el-form-item>
          <el-button type="primary" icon="el-icon-plus" @click="add">
            新增
          </el-button>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-edit"
            :disabled="disabled"
            @click="edit"
          >
            编辑
          </el-button>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-view"
            :disabled="disabled"
            @click="view"
          >
            查看详情
          </el-button>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-delete"
            :disabled="deleteDisabled"
            @click="deleteCode"
          >
            删除
          </el-button>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-document-checked"
            :disabled="deleteDisabled"
            @click="enable('0')"
          >
            启用
          </el-button>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-document-delete"
            :disabled="deleteDisabled"
            @click="enable('1')"
          >
            禁用
          </el-button>
        </el-form-item>
      </el-form> -->

      <div class="result">
        <el-table
          ref="multipleTable"
          v-loading="loading"
          border
          :data="tableData"
          tooltip-effect="dark"
          style="width: 100%"
          align="center"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55" />
          <el-table-column prop="sort" label="序号" width="200"/>
          <el-table-column prop="value" label="名称" />
          <el-table-column label="状态" width="200">
            <template slot-scope="scope">
              <el-tag size="medium">{{
                scope.row.status | showStatus
              }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="创建时间" width="200">
            <template slot-scope="scope">
              {{ scope.row.createTime | parseDate }}
            </template>
          </el-table-column>
        </el-table>
        <div class="page">
          <el-pagination
            background
            :current-page.sync="currentPage"
            layout="total,prev, pager, next, sizes"
            :total="total"
            :page-sizes="[10, 20, 50, 100]"
            :page-size="pageSize"
            @current-change="handleCurrentChange"
            @size-change="handleSizeChange"
          />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { pageMixin } from "../../../mixins/index";
import { getList, remove, enable } from "../../../api/system";
import { EquipmentStatus } from "../../../utils/resource";
export default {
  filters: {
    parseDate(value) {
      if (value) {
        return value.replace("T", " ");
      }
      return null;
    },
    showStatus(value) {
      if (value != null) {
        return EquipmentStatus.find((p) => p.value === value.toString())?.label;
      }
      return null;
    },
  },
  mixins: [pageMixin],
  data() {
    return {
      formInline: {
        content: "",
      },
      toolbtn: [] // 列表顶部按钮
    };
  },
  async created() {
    await this.init();
  },
  methods: {
    // 列表顶部功能按钮点击事件
    showViews(action){
      //console.log(action);
      switch(action)
      {
        case '新增':
          this.add();
          break;
        case '编辑':
          if(this.ids.length != 1) {
            this.$message.error('此功能仅限操作1条数据！');
            return;
          }
          this.edit();
          break;
        case '查看详情':
          if(this.ids.length != 1) {
            this.$message.error('此功能仅限操作1条数据！');
            return;
          }
          this.view()
          break;
        case '删除':
          if(this.ids.length != 1) {
            this.$message.error('此功能仅限操作1条数据！');
            return;
          }
          this.deleteCode();
          break;
        case '启用':
          if(this.ids.length != 1) {
            this.$message.error('此功能仅限操作1条数据！');
            return;
          }
          this.enable('0');
          break;
        case '禁用':
          if(this.ids.length != 1) {
            this.$message.error('此功能仅限操作1条数据！');
            return;
          }
          this.enable('1');
          break;
      }
    },
    async enable(value) {
      let confimText = "";
      if (value === "0") {
        confimText = "启用";
      } else {
        confimText = "禁用";
      }
      this.$confirm(
        `此操作将${confimText}这些废物安全告知卡, 是否继续?`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).then(async() => {
        const ids = this.ids.map((p) => p.code);
        await enable(ids, value);
        await this.init();
        this.$message({
          type: "success",
          message: `${confimText}成功!`,
        });
      });
    },
    async deleteCode() {
      this.$confirm("此操作将永久删除废物安全告知卡, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(async() => {
        const ids = this.ids.map((p) => p.code);
        await remove(ids);
        await this.init();
        this.$message({
          type: "success",
          message: "删除成功!",
        });
      });
    },
    view() {

      this.$router.push(`/system/safetynotification/view/${this.code}`);
    },
    edit() {
      this.$router.push(`/system/safetynotification/edit/${this.code}`);
    },
    add() {
      this.$router.push("/system/safetynotification/create");
    },
    async reset() {
      this.formInline = {
        content: "",
      };
      await this.init();
    },
    async init() {
      this.tableData = [];
      this.loading = true;
      const { data, count } = await getList({
        pageSize: this.pageSize,
        currentPage: this.currentPage,
        content: this.formInline.content,
        type: "104",
      });
      this.tableData = data;
      this.total = count;
      this.loading = false;

      // 初始化按钮数据
      this.toolbtn = JSON.parse(localStorage.getItem('废物安全告知卡管理'));
      //console.log(this.toolbtn);
    },
  },
};
</script>

<style lang="scss" scoped>
.result {
  margin-top: 10px;
}

.show-result {
  margin-top: 10px;
}

.abow_dialog {
  display: flex;
  justify-content: center;
  align-items: Center;
  overflow: hidden;
  .el-dialog {
    margin: 0 auto !important;
    height: 90%;
    overflow: hidden;
    .el-dialog__body {
      position: absolute;
      left: 0;
      top: 54px;
      bottom: 0;
      right: 0;
      padding: 0;
      z-index: 1;
      overflow: hidden;
      overflow-y: auto;
    }
  }
}
</style>
